package data

import (
	"061/Sgame/db"
	"061/Sgame/net"
	"061/Sgame/server/game/gameConfig/general"
	"061/Sgame/server/game/model"
	"encoding/json"
	"gorm.io/gorm"
	"log"
	"time"
)

const (
	GeneralNormal      = 0 //正常
	GeneralComposeStar = 1 //星级合成
	GeneralConvert     = 2 //转换
)
const SkillLimit = 3

var GeneralDao = &generalDao{
	genChan: make(chan *General, 100),
}

type generalDao struct {
	genChan chan *General
}

func (g *generalDao) running() {
	for {
		select {
		case gen := <-g.genChan:
			if gen.Id > 0 {
				err := db.GormDB.Model(gen).Where("id=?", gen.Id).Select(
					"level", "exp", "order", "cityId",
					"physical_power", "star_lv", "has_pr_point",
					"use_pr_point", "force_added", "strategy_added",
					"defense_added", "speed_added", "destroy_added",
					"parentId", "compose_type", "skills", "state").Updates(gen).Error
				if err != nil {
					log.Println("generalDao running error", err)
				}
			}
		}
	}

}

func init() {
	go GeneralDao.running()
}

type General struct {
	Id            int             `gorm:"column:id;primaryKey"`
	RId           int             `gorm:"column:rid"`
	CfgId         int             `gorm:"column:cfgId;comment:城市id"`
	PhysicalPower int             `gorm:"column:physical_power;comment:体力"`
	Level         int8            `gorm:"column:level;comment:level"`
	Exp           int             `gorm:"column:exp;comment:经验"`
	Order         int8            `gorm:"column:order;comment:第几队"`
	CityId        int             `gorm:"column:cityId;comment:城市id"`
	CreatedAt     time.Time       `gorm:"column:created_at"`
	CurArms       int             `gorm:"column:arms;comment:兵种"`
	HasPrPoint    int             `gorm:"column:has_pr_point;comment:总属性点"`
	UsePrPoint    int             `gorm:"column:use_pr_point;comment:已用属性点"`
	AttackDis     int             `gorm:"column:attack_distance;comment:攻击距离"`
	ForceAdded    int             `gorm:"column:force_added;comment:已加攻击属性"`
	StrategyAdded int             `gorm:"column:strategy_added;comment:已加战略属性"`
	DefenseAdded  int             `gorm:"column:defense_added;comment:已加防御属性"`
	SpeedAdded    int             `gorm:"column:speed_added;comment:已加速度属性"`
	DestroyAdded  int             `gorm:"column:destroy_added;comment:已加破坏属性"`
	StarLv        int8            `gorm:"column:star_lv;comment:稀有度(星级)进阶等级"`
	Star          int8            `gorm:"column:star;comment:稀有度(星级)"`
	ParentId      int             `gorm:"column:parentId;comment:已合成到武将的id"`
	Skills        string          `gorm:"column:skills;comment:携带的技能"`
	SkillsArray   []*model.GSkill `gorm:"-"`
	State         int8            `gorm:"state;comment:0:正常，1:转换掉了"`
}

func (g *General) TableName() string {
	return "general"
}
func (g *General) ToModel() interface{} {
	p := model.General{}
	p.CityId = g.CityId
	p.Order = g.Order
	p.PhysicalPower = g.PhysicalPower
	p.Id = g.Id
	p.CfgId = g.CfgId
	p.Level = g.Level
	p.Exp = g.Exp
	p.CurArms = g.CurArms
	p.HasPrPoint = g.HasPrPoint
	p.UsePrPoint = g.UsePrPoint
	p.AttackDis = g.AttackDis
	p.ForceAdded = g.ForceAdded
	p.StrategyAdded = g.StrategyAdded
	p.DefenseAdded = g.DefenseAdded
	p.SpeedAdded = g.SpeedAdded
	p.DestroyAdded = g.DestroyAdded
	p.StarLv = g.StarLv
	p.Star = g.Star
	p.State = g.State
	p.ParentId = g.ParentId
	p.Skills = g.SkillsArray
	return p
}

//func (g *General) AfterFind(tx *gorm.DB) (err error) {
//	if g.name == "skills"{
//		g.SkillsArray = make([]*model.GSkill, 3)
//		if cell != nil{
//			gs, ok := (*cell).([]uint8)
//			if ok {
//				json.Unmarshal(gs, &g.Skills)
//				fmt.Println(g.SkillsArray)
//			}
//		}
//	}
//}

func (g *General) beforeModify() {
	data, _ := json.Marshal(g.SkillsArray)
	g.Skills = string(data)
}

func (g *General) BeforeCreate(tx *gorm.DB) (err error) {
	g.beforeModify()
	return nil
}

func (g *General) BeforeUpdate(tx *gorm.DB) (err error) {
	g.beforeModify()
	return nil
}

func (g *General) SyncExecute() {
	GeneralDao.genChan <- g
	g.Push()
}

func (g *General) GetDestroy() int {
	cfg, ok := general.GeneralConf.GMap[g.CfgId]
	if ok {
		return cfg.Destroy + cfg.DestroyGrow*int(g.Level) + g.DestroyAdded
	}
	return 0
}

func (g *General) GetForce() int {
	cfg, ok := general.GeneralConf.GMap[g.CfgId]
	if ok {
		return cfg.Force + cfg.ForceGrow*int(g.Level) + g.ForceAdded
	}
	return 0
}

func (g *General) GetStrategy() int {
	cfg, ok := general.GeneralConf.GMap[g.CfgId]
	if ok {
		return cfg.Strategy + cfg.StrategyGrow*int(g.Level) + g.StrategyAdded
	}
	return 0
}
func (g *General) GetSpeed() int {
	cfg, ok := general.GeneralConf.GMap[g.CfgId]
	if ok {
		return cfg.Speed + cfg.SpeedGrow*int(g.Level) + g.SpeedAdded
	}
	return 0
}
func (g *General) GetDefense() int {
	cfg, ok := general.GeneralConf.GMap[g.CfgId]
	if ok {
		return cfg.Defense + cfg.DefenseGrow*int(g.Level) + g.DefenseAdded
	}
	return 0
}

/* 推送同步 begin */
func (g *General) IsCellView() bool {
	return false
}

func (g *General) IsCanView(rid, x, y int) bool {
	return false
}

func (g *General) BelongToRId() []int {
	return []int{g.RId}
}

func (g *General) PushMsgName() string {
	return "general.push"
}

func (g *General) Position() (int, int) {
	return -1, -1
}

func (g *General) TPosition() (int, int) {
	return -1, -1
}

func (g *General) Push() {
	net.Mgr.Push(g)
}
